﻿#include "inbound.h"
#include "ui_inbound.h"

Inbound::Inbound(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Inbound)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());
    comboboxinit();//读取数据库商品编号到combo box
}

Inbound::~Inbound()
{
    delete ui;
}

void Inbound::on_inputButton_clicked()
{
    //获取combo box控件里的值
    QString id=ui->idcomboBox->currentText();
    //QMessageBox::information(this,"提示",id);

    //判断入库商品是否为空
    if(ui->amountlineEdit->text().isEmpty())
    {
        QMessageBox::information(this,"提示","商品入库数量不能为空，请重试");
        ui->amountlineEdit->setFocus();
        return;
    }
    QSqlQuery sqlquery;
    QString strid="StockId=";
    strid+=id;
    QString str=QString("select *from commoditydatatable where %1").arg(strid);
    //QMessageBox::information(this,"提示",str);
    sqlquery.exec(str);

    int i=0;
    QString strAmount;

    while(sqlquery.next())
    {
        strAmount=sqlquery.value(2).toString();
    }
    int inputamount=ui->amountlineEdit->text().toInt();
    int tableamount=strAmount.toUInt();
    int isum=inputamount+tableamount;

    QString strisum=QString::number(isum);
    //更新数据表中数量字段
    QString strdb=QString("update commoditydatatable set StockAmount=%1 where %2").arg(strisum,strid);
    if(sqlquery.exec(strdb))
    {
        QMessageBox::information(this,"提示","入库成功");
    }
    else
    {
        QMessageBox::information(this,"提示","入库失败");
    }

}


void Inbound::on_exitButton_clicked()
{
    close();
}

//初始化combo box
void Inbound::comboboxinit()
{
    int i=0;
    QSqlQuery sqlQuery;
    sqlQuery.exec("select *from commoditydatatable");

    QString id;
    while(sqlQuery.next())
    {
        id=sqlQuery.value(0).toString();
        ui->idcomboBox->insertItem(i,id);
        i++;
    }
}
